﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.Model;
using AD.BLL;

public partial class Manager_Ad_KeyWords : KeyWordsPageBase
{
    public KeyWordsMod keyWord = new KeyWordsMod();
    public string TitleSearchEngineType = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        if (!string.IsNullOrEmpty(action))
        {
            if (action.Equals("edit"))
            {
                string strId = Request.QueryString["id"];
                int id;
                if (int.TryParse(strId, out id))
                {
                    if (!Page.IsPostBack)
                    {
                        SetDataBind(id);
                        TitleSearchEngineType = "修改" + GetSearchEngineType(keyWord.SearchEngineType);
                    }
                    else
                    {
                        KeyWordsMod tmpKeyWords = ToModel();
                        if (HttpDataCache.AppSettings.IsChangeAudit)
                            tmpKeyWords.Status = KeyWordsStatus.WaitPass;
                        if (KeyWordsBLL.UpdateInfo(tmpKeyWords) > 0)
                        {
                            AlertRedirect("修改关键字成功", "KeywordsList.aspx?action=gopage&pageIndex=1");
                        }
                        else
                        {
                            Alert("修改关键字失败");
                        }
                    }
                }
            }
            else if (action.Equals("add"))
            {
                string strId = Request.QueryString["typeId"];
                int searchEngineType;
                int.TryParse(strId, out searchEngineType);
                if (!Page.IsPostBack)
                {
                    TitleSearchEngineType = "增加" + GetSearchEngineType((SearchEngineType)searchEngineType);
                }
                else
                {
                    KeyWordsMod tmpKeyWords = ToModel();
                    tmpKeyWords.SearchEngineType = (SearchEngineType)searchEngineType;
                    if (KeyWordsBLL.Insert(tmpKeyWords) > 0)
                    {
                        AlertRedirect("增加关键字成功", "KeywordsList.aspx?action=gopage&pageIndex=1");
                        //RedirectNewURL("KeywordsList.aspx?action=gopage&pageIndex=1");

                    }
                    else
                    {
                        Alert("增加关键字失败");
                    }
                }
            }
        }
    }

    KeyWordsMod ToModel()
    {
        int autoId = 0;
        int.TryParse(Request.Params["AutoID"], out autoId);
        KeyWordsMod tmpKeyWord = null;
        if (autoId > 0)
        {
            tmpKeyWord = KeyWordsBLL.QueryById(autoId);
        }
        else
        {
            tmpKeyWord = new KeyWordsMod();
            tmpKeyWord.UserId = SessionUser.AutoID;
        }
        tmpKeyWord.AdDescribe_First = Request.Params["AdDescribe_First"];
        tmpKeyWord.AdDescribe_Second = Request.Params["AdDescribe_Second"];
        tmpKeyWord.AdTitle = Request.Params["AdTitle"];
        tmpKeyWord.AdUrl = Request.Params["AdUrl"];
        tmpKeyWord.AdUrl_Title = Request.Params["AdUrl_Title"];
        tmpKeyWord.AutoID = autoId;
        tmpKeyWord.Commit_Date = DateTime.Now;
        tmpKeyWord.KeyWords = Request.Params["KeyWords"];
        return tmpKeyWord;
    }

    void SetDataBind(int id)
    {
        KeyWordsMod tmp = KeyWordsBLL.QueryById(id);
        if (tmp != null)
            keyWord = tmp;
    }
}